import time

import event.event
import logo.Mlog
import match.photo


class Step:
    # 失败次数
    fail_numb = 5
    # 失败次数
    curr_fail_numb = 0
    # 当匹配失败时是否跳过这一步骤（当疲劳超过300，会出现提示）/0不能，1可以
    fail_skip: int = 0
    # 当前任务序列
    index: int = 0
    is_exit: bool = False
    name: str = None
    img_path: [str] = []
    is_click: bool = False
    # 任务是否完成
    __is_complete: bool = False
    x: int = None
    y: int = None

    def check(self,isClick:bool=True) -> bool:
        # if not self.x is None and not self.y is None:
        #     event.event.click_icons_pot(self.x, self.y)
        #     time.sleep(0.2)
        #     return True
        # 截屏
        screenshot = event.event.take_screenshot()
        # 根据屏幕匹配结果，判断是否找到了
        list_match, shot = match.photo.find_icons_semblance_screenshot(screenshot, [self.img_path])
        if list_match:
            if not isClick:
                self.__is_complete = True
                return True
            if not self.is_click:
                self.x, self.y = list_match[0].center_x, list_match[0].center_y
                event.event.click_icons(list_match[0])
            self.__is_complete = True
            print("匹配成功=", self.img_path)
        else:
            # 没有匹配成功判断是否可以跳过
            if self.fail_skip:
                self.__is_complete = True
                logo.Mlog.logger.debug("没有匹配成功但是可以跳过")
            else:
                self.curr_fail_numb += 1
                print("没有匹配成功=", self.img_path)
                self.__is_complete = False
        return self.__is_complete

    # 加载图片
    def get_image_paths(self, folder_path):
        # 打印所有图片路径
        self.img_path = folder_path
        # logo.Mlog.logger.debug(self.img_path)
        self.fail_skip = int(self.img_path.split("_")[2].split(".")[0])

    pass
